//
//  CWNavigationController.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/22.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit

class CWNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置边缘手势代理
        interactivePopGestureRecognizer?.delegate = self
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            if viewControllers.count == 1 {
                
                let title = viewControllers.first!.title
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!,imageNamed: "navigationbar_back_withtext", target: self, action: #selector(clickPushButton))
                
            }else{
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageNamed: "navigationbar_back_withtext",target: self, action: #selector(clickPushButton))
            }
            viewController.title = "当前是第\(viewControllers.count + 1)级控制器"
            
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func clickPushButton() {
        
        popViewController(animated: true)
        
    }
    
    //MARK: 实现边缘手势代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1 {
            
            return false
            
        }
        
        return true
    }

}
